home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-03-02 | 2.1 KB | 83 lines | [TEXT/GEOL] |
- Item 9905085 1-March-90 15:44PST
-
- From: FARALLON.ENG Farallon, Engineering,PRT
-
- To: CPLUS.DEV$ C++ Interest List--Developers
-
- Sub: Waste in Constructor call
-
-
- (This may seem like a trivial point, but we bring it up because it's
- constructors are fundamental to writing applications and show up
- everywhere.)
-
- We're creating a simple object for doing fixed-point integer math.
- Here's some of the C++ class description:
-
- class TFixed
- {
- public:
- TFixed( const int );
- ~TFixed( void );
- ...
- private:
- long fValue;
- };
-
- inline TFixed::TFixed( const int value )
- {
- fValue = value << 16; // machine dependent
- }
-
- inline TFixed::~TFixed()
- {
- }
-
- Here's a very simple test driver:
-
- void main( void )
- {
- TFixed a = 1;
- TFixed b(2);
- }
-
- We would estimate that the code for this would be very simple. Unfortunately,
- it always seems to generate an extra instruction. After compiling with CPlus,
- DumpObj produces:
-
- LINK A6,#$FFF8
- MOVE.L #$00010000,-$0008(A6)
- LEA -$0008(A6),A0
- MOVE.L #$00020000,-$0004(A6)
- LEA -$0004(A6),A0
- UNLK A6
- RTS
-
- What are the LEA instructions there for? They put a value into A0 that's never
- used. Looking at the intermediate C code gives us a clue:
-
-
- void main(void ){
- {
- struct TFixed a;
- struct TFixed b;
- ( ((& a)-> fValue= (((int )1)<< 16)), (& a)) ;
- ( ((& b)-> fValue= (((int )2)<< 16)), (& b)) ;
- ( (( (( (( 0) ), 0) ), 0) )) ;
- ( (( (( (( 0) ), 0) ), 0) )) ;
- }
- }
-
- Since a constructor always returns the address of the object it's
- initializing, it always gets dumped onto A0. Is there a way to
- make the MPW C optimize these out? or a way to prevent CFront from
- generating them for stack-based objects (that don't need a value
- returned)?
-
- (It's interesting to note that the line "TFixed c = b;" doesn't generate the
- extra LEA.)
-
- Anybody else noticed this?
-
-
-